<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastWarning, Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils';
  import serachUserid from '@/components/serachUserid/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import table2excel from 'js-table2excel';
  const action = reactive({
    total:0,
    list:[] as any[],
    allList:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis')),
  })
  const initialQuery = {
    page:1,
    limit:20,
    user_id:''
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = () => {
    getList();
  };
  const resetQuery = () => {
    action.timeObj = getPlayEndTime(getDateArray('ymdhis'))
    Object.assign(listQuery, {
      ...initialQuery
    });
    getList()
  };
  const handleDownload = async()=>{
    if(action.total == 0) return ToastWarning('暂无可导出数据')
    const loading = Toastloading('数据导出中')
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await propApi.luckGiftUserStatistic({
      ...listQuery,
      page:1,
      date_st,
      date_ed,
      limit:action.total
    })
    loading.close()
    const { list=[] } = res.data
    const data = list.map((res:any)=>{
      if(res['output_ratio'] = `${res.output_ratio}%`)
      return res
    })
    const tHeader = [
      { title: '用户UUID', key: "uuid"},
      { title: '用户昵称', key: "nickname"},
      { title: '礼物消耗钻石总数', key: "u_diamond_num"},
      { title: '返奖钻石数', key: "r_diamond_num"},
      { title: '损益', key: "profit_loss"},
      { title: '回馈率', key: "output_ratio"},
    ]
    table2excel(
      tHeader,
      data,
      `新·幸运礼物参与用户统计`)
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await propApi.luckGiftUserStatistic({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { list=[], total=0, all={} } = res?.data || {}
    action.total = total
    action.list = list
    action.allList = [all]
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="warning" @click="handleDownload">导出列表</el-button>
        </div>
      </div>
    </div>
    <el-table
      style="width: max-content;; min-height: 102px"
      class="xm-TableCenter"
      :data="action.allList"
      border
    >
      <el-table-column width="100" prop="user_num" label="用户数量" />
      <el-table-column min-width="100" prop="u_diamond_num" label="礼物消耗钻石总数" />
      <el-table-column min-min-width="100" prop="r_diamond_num" label="返奖钻石数" />
      <el-table-column min-width="120" prop="profit_loss" label="损益" />
      <el-table-column min-width="120" prop="anchor_num" label="主播分成" />
      <el-table-column min-width="120" prop="platform_num" label="平台流入" />
      <el-table-column min-width="120" prop="output_ratio" label="回馈率">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
    </el-table>
    <el-table
      style="margin-top:40px;"
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="uuid" label="用户UUID" />
      <el-table-column min-width="140" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column min-width="100" prop="u_diamond_num" label="礼物消耗钻石总数" />
      <el-table-column min-min-width="100" prop="r_diamond_num" label="返奖钻石数" />
      <el-table-column min-width="120" prop="profit_loss" label="损益" />
      <el-table-column min-width="120" prop="anchor_num" label="主播分成" />
      <el-table-column min-width="120" prop="platform_num" label="平台流入" />
      <el-table-column min-width="120" prop="output_ratio" label="回馈率">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>
